package com.nf.health.app.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.main_intervention.activity.FoodPlanActivity;
import com.nf.health.app.main_intervention.activity.MeridianActivity;
import com.nf.health.app.main_intervention.activity.MusicActivity;
import com.nf.health.app.main_intervention.activity.RunActivity;
import com.nf.health.app.models.SolutionSchemeBean;
import com.nf.health.app.utils.ActivityUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 解决方案中item适配器
 * Created by hepan on 2016/3/31.
 */
public class HomeSolutionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context cnt;
    private final List<SolutionSchemeBean> solutionBeans;

    public HomeSolutionAdapter(Context cnt, List<SolutionSchemeBean> solutionBeans) {
        this.solutionBeans = solutionBeans;
        this.cnt = cnt;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.item_home_solution, null, false);//第二个参数传parent导致item只显示一个 为什么???? 若RecyclerView外面包裹一层布局,既可以传parent
        return new SolutionViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        SolutionViewHolder solutionViewHolder = (SolutionViewHolder) holder;
        SolutionSchemeBean bean = solutionBeans.get(position);
        solutionViewHolder.tvTime.setText(bean.getTime() + "  " + bean.getHour() + "  " + bean.getTitle());
        solutionViewHolder.tvContent.setText(bean.getName());
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return solutionBeans == null ? 0 : solutionBeans.size();
    }

    public class SolutionViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_time)
        TextView tvTime;
        @Bind(R.id.tv_content)
        TextView tvContent;
        @Bind(R.id.btn_check)
        TextView btnCheck;
        @Bind(R.id.rl_parent)
        RelativeLayout rlParent;

        public SolutionViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            rlParent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    String type = solutionBeans.get(position).getType();
                    String id = solutionBeans.get(position).getId();
                    Bundle b1 = new Bundle();
                    b1.putString(Constants.INTERVENTION_ID,id);
                    switch (type) {
                        case Constants.INTERVENTION_MEDICINEDDIET://膳食
                            ActivityUtils.showActivity(cnt, FoodPlanActivity.class,b1);
                            break;
                        case Constants.INTERVENTION_MUSIC://音乐
                            ActivityUtils.showActivity(cnt, MusicActivity.class,b1);
                            break;
                        case Constants.INTERVENTION_SPORT://运动
                            ActivityUtils.showActivity(cnt, RunActivity.class,b1);
                            break;
                        case Constants.INTERVENTION_MERIDIAN://经络
                            ActivityUtils.showActivity(cnt, MeridianActivity.class,b1);
                            break;
                        default:
                            break;
                    }
                }
            });
        }
    }
}
